package cn.mifengkong.appcommon.util;

import android.content.Context;
import android.text.TextUtils;

import com.mifengkong.frtools.jsbridge.CallBackFunction;

import org.json.JSONException;
import org.json.JSONObject;

import java.net.URLDecoder;

import cn.mifengkong.appcommon.constant.DeepLinkForNativeFunctionConstant;
import cn.mifengkong.appcommon.webview.InfoToJsonUtil;
import cn.mifengkong.appcommon.webview.listener.JsBridgeCallBackListener;
import cn.mifengkong.appcommon.webview.listener.MyStartActivityResultForJsbridgeListener;

/**
 * Created by jiangyongxing on 2018/6/27.
 * 描述：
 */

public class DeepLinkForNatvieUrlUtil {


    /**
     * 需要跳转什么界面
     *
     * @param nativeUrl
     * @return 跳转到哪个页面
     */
    public static String nativeUrlToActivityOrFunction(String nativeUrl) {
        //如果if大于5个的话  则采用switch  如果小于五个的话  可以采用if
        if (TextUtils.isEmpty(nativeUrl)) {
            //默认返回
            return DeepLinkForNativeFunctionConstant.NATIVE_URL_START_ACTIVITY;
        }
        for (String url : DeepLinkForNativeFunctionConstant.nativeUrls) {
            if (nativeUrl.contains(url)) {
                return url;
            }
        }
        return DeepLinkForNativeFunctionConstant.NATIVE_URL_START_ACTIVITY;
    }

    /**
     * 分割、提取参数
     *
     * @param nativeUrl
     * @return
     */
    public static String[] splitNativeUrl(String nativeUrl) {
        if (TextUtils.isEmpty(nativeUrl)) {
            return new String[]{};
        }
        nativeUrl = nativeUrl.replace("native://", "");
        return nativeUrl.split("\\?");
    }

    /**
     * 根据名称和参数打开相对的界面
     *
     * @param context
     * @param nameAndParamters
     * @param applicationId
     * @param function
     */
    public static void startActivity(Context context, String[] nameAndParamters, String applicationId, CallBackFunction function) {
        if (nameAndParamters == null || nameAndParamters.length < 1) {
            InfoToJsonUtil.assemblyIntoFailureJson(function, new JSONObject());
        } else if (nameAndParamters.length < 2) {
            ActivityStartUtil.startActivityByActivityNameAndParameter(context, nameAndParamters[0], "", applicationId, new MyStartActivityResultForJsbridgeListener(function));
        } else {
            ActivityStartUtil.startActivityByActivityNameAndParameter(context, nameAndParamters[0], nameAndParamters[1], applicationId, new MyStartActivityResultForJsbridgeListener(function));
        }
    }


    /**
     * 根据名称和参数打开相对的界面
     *
     * @param context
     * @param nameAndParamters
     * @param applicationId
     */
    public static void startActivity(Context context, String[] nameAndParamters, String applicationId) {
        startActivity(context, nameAndParamters, applicationId, null);
    }

    public static void startActivityOrFunction(Context context, String[] nameAndParamters, String nativeUrlCode,
                                               String applicationId, CallBackFunction function, JsBridgeCallBackListener listener) {
        if (DeepLinkForNativeFunctionConstant.NATIVE_URL_START_ACTIVITY.equals(nativeUrlCode)) {
            //直接打开Activity

            //如果是卡管家类型的包，跳转的登录时dialog，要区别对待
            if (listener != null && listener.userDialogLoginType(nameAndParamters)) {
                listener.startCardManagerLoginDialog();
            } else {
                startActivity(context, nameAndParamters, applicationId, function);
            }
        } else {
            startFunction(nativeUrlCode, nameAndParamters, listener, function);
        }
    }

    private static void startFunction(String nativeUrlCode, String[] nameAndParamters, JsBridgeCallBackListener listener, CallBackFunction function) {
        if (listener == null) {
            InfoToJsonUtil.assemblyIntoFailureJson(function, new JSONObject());
            return;
        }
        switch (nativeUrlCode) {
            case DeepLinkForNativeFunctionConstant.NATIVE_URL_KEFUCHAT:
                listener.startCustomerService();
                InfoToJsonUtil.assemblyIntoFailureJson(function, new JSONObject());
                break;
            case DeepLinkForNativeFunctionConstant.NATIVE_URL_SHARE:
                if (nameAndParamters == null || nameAndParamters.length < 2 || TextUtils.isEmpty(nameAndParamters[1])) {
                    InfoToJsonUtil.assemblyIntoFailureJson(function, new JSONObject());
                } else {
                    JSONObject jsonObject = new JSONObject();
                    String[] parameters = nameAndParamters[1].split("&");
                    try {
                        for (String s : parameters) {
                            String[] map = s.split("=");
                            if (!TextUtils.isEmpty(map[0]) && !TextUtils.isEmpty(map[1])) {
                                jsonObject.put(map[0], URLDecoder.decode(map[1]));
                            }
                        }
                        listener.shareUmeng(jsonObject.toString());
                    } catch (JSONException e) {
                        e.printStackTrace();
                        InfoToJsonUtil.assemblyIntoFailureJson(function, new JSONObject());
                    }
                }
                break;
            default:
                break;
        }
    }

}
